package com.microcity.autocomponent.autoview.viewex;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/5/16.
 */

public class DemoView extends View {

    private Paint mPaint = new Paint();

    public DemoView(Context context) {
        this(context,null,0);
    }

    public DemoView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
        initPaint();
    }

    public DemoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    private void initPaint() {
        mPaint.setColor(Color.GREEN);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(20f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPaintStyle(canvas);
    }


    //---------------以下是画笔的使用及设置---------------------
    private void initPaintStyle(Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(200,200,100,mPaint);

        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeWidth(50);
        canvas.drawCircle(200,500,100,mPaint);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        canvas.drawCircle(200,800,100,mPaint);

    }

    //---------------以下是点线图形的绘制---------------------
    private void initPoint(Canvas canvas) {
        //画点1.一个坐标 2.一组数据偶数多个点 2的1-n倍
        canvas.drawPoint(400,400,mPaint);
        canvas.drawPoints(new float[]{20,20,200,200,50,50,420,420,20,40,20,50},mPaint);
    }

    private void initLine(Canvas canvas) {
        //画线1.两个点确定一条线 2.一组数据 每四个数据组成两个点 画一条线 4的1-n倍
        canvas.drawLine(120,120,500,500,mPaint);
        canvas.drawLines(new float[]{20,30,30,400,0,0,120,210},mPaint);
    }

    private void initRect(Canvas canvas) {
        //画矩形 1.两个点确定一个矩形
        canvas.drawRect(100,100,400,600,mPaint);
        Rect rect = new Rect(100,100,400,600); //int 精度问题
        RectF rectF = new RectF(100,100,400,600);//float
        canvas.drawRect(rect,mPaint);
        canvas.drawRect(rectF,mPaint);
    }
    private void initRoundRect(Canvas canvas) {
        //画圆角矩形
        RectF rect = new RectF(100,200,800,400);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(rect,mPaint);
        mPaint.setColor(Color.BLUE);
        //实际上在rx为宽度的一半，ry为高度的一半时，刚好是一个椭圆，通过上面我们分析的原理推算一下就能得到，而当rx大于宽度的一半，ry大于高度的一半时，实际上是无法计算出圆弧的，所以drawRoundRect对大于该数值的参数进行了限制(修正)，凡是大于一半的参数均按照一半来处理。
        canvas.drawRoundRect(rect,700,400,mPaint);//多出了两个参数rx=700 ry=400  能画出椭圆 但是rx ry都是有限制的
    }
    private void initOval(Canvas canvas) {
        //画椭圆 矩形的内切图形
        RectF rectF = new RectF(100,100,400,800);
        canvas.drawOval(rectF,mPaint);
    }

    private void initCircle(Canvas canvas) {
        //画圆
        canvas.drawCircle(200,200,100,mPaint);
    }

    private void initOvalAngle(Canvas canvas) {
        //画圆弧 椭圆圆弧 startAngle开始角度  sweepAngle扫过角度  useCenter是否使用中心
        RectF rectF = new RectF(100,100,800,400);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(rectF,mPaint);

        mPaint.setColor(Color.YELLOW);
        canvas.drawArc(rectF,0,90,false,mPaint);

        RectF rectF2 = new RectF(100,600,800,900);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(rectF2,mPaint);

        mPaint.setColor(Color.YELLOW);
        canvas.drawArc(rectF2,0,90,true,mPaint);
    }

    private void initCircleAngel(Canvas canvas) {
        //画圆弧 圆形圆弧
        RectF rectF = new RectF(100,100,600,600);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(rectF,mPaint);

        mPaint.setColor(Color.YELLOW);
        canvas.drawArc(rectF,0,90,false,mPaint);

        RectF rectF2 = new RectF(100,700,600,1200);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(rectF2,mPaint);

        mPaint.setColor(Color.YELLOW);
        canvas.drawArc(rectF2,0,90,true,mPaint);
    }




}
